package com.hujesse.controller;

import com.google.gson.Gson;
import com.hujesse.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class DeptConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    // private static final String REST_URL_PREFIX="http://localhost:8001";
    //Ribbon:我们这里的地址，应该是一个变量，通过服务名来访问,达到了解耦
     private static final String REST_URL_PREFIX="http://springcloud-provider-dept";

    @RequestMapping("/consumer/dept/get/{id}")
    public String getId(@PathVariable("id") Long id){
       Dept dept = restTemplate.getForObject(REST_URL_PREFIX + "/get/" + id, Dept.class);
        return new Gson().toJson(dept);
    }

    @RequestMapping("/add/{name}")
    public String addDept( Dept dept,@PathVariable("name") String name){
        System.out.println(dept);
        dept.setDeptName(name);
        String s = restTemplate.postForObject(REST_URL_PREFIX+"/add",dept,String.class);
        return s;
    }
    @RequestMapping("/dept/list")
    public String getList(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",String.class);
    }
    @RequestMapping("/hello")
    public String a(){
        return "helloWOlds";
    }

}
